package com.abc.cloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.abc.cloud.common.exception.MyException;
import com.abc.cloud.dto.role.RolePage;
import com.abc.cloud.dto.user.UserAndAllRole;
import com.abc.cloud.entity.MenuEntity;
import com.abc.cloud.entity.PermsEntity;
import com.abc.cloud.entity.RoleEntity;
import com.abc.cloud.vo.role.RoleSave;
import com.abc.cloud.vo.role.RoleUpdate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.RoleDao;
import com.abc.cloud.service.RoleService;
import org.springframework.transaction.annotation.Transactional;


@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<RoleEntity> page = this.page(
                new Query<RoleEntity>().getPage(params),
                new QueryWrapper<RoleEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public RoleEntity getRoleInfo(Long roleId) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("id", roleId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public RoleEntity saveRoleInfo(RoleSave roleSave) {
        RoleEntity tbRole = BeanUtil.copyProperties(roleSave, RoleEntity.class);
        int insert = this.baseMapper.insert(tbRole);
        if(insert <= 0){
            return null;
        }
        return this.getRoleInfo(tbRole.getId());
    }

    @Override
    public RoleEntity updateRoleInfo(RoleUpdate roleUpdate) {
        RoleEntity tbRole = this.getRoleInfo(roleUpdate.getId());
        BeanUtils.copyProperties(roleUpdate, tbRole);
        int insert = this.baseMapper.updateById(tbRole);
        if(insert <= 0){
            return null;
        }
        return this.getRoleInfo(tbRole.getId());
    }

    @Override
    public Boolean deleteRoleInfo(Long roleId) {
        RoleEntity tbRole = this.getRoleInfo(roleId);
        tbRole.setStatus(2);
        int insert = this.baseMapper.updateById(tbRole);
        if(insert <= 0){
            return false;
        }
        return true;
    }

    @Override
    public List<RoleEntity> getRoleList() {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public RolePage getRolePage(Integer currentPag, Integer pageSize, String content) {
        RolePage rolePage = new RolePage();
        rolePage.setCurrentPag(currentPag);
        rolePage.setPageSize(pageSize);
        rolePage.setTotalPag(this.baseMapper.queryRoleTotal());
        rolePage.setRoleList(this.baseMapper.queryRoleInfoPage((currentPag - 1) * pageSize, pageSize, content));
        return rolePage;
    }

    @Override
    public List<MenuEntity> getRoleMenuList(Long roleId) {
        return this.baseMapper.queryRoleMenuList(roleId);
    }

    @Transactional
    @Override
    public Boolean updateRoleMenu(List<Long> adminMenuIds, Long roleId) {
        try{
            // 删除该角色以及有的后台菜单
            this.baseMapper.deleteRoleMenuByRoleId(roleId);
            // 添加传来的后台菜单
            this.baseMapper.saveRoleMenu(roleId, adminMenuIds);
        } catch (Exception e){
            throw new MyException(e.toString());
        }
        return true;
    }

    @Override
    public List<PermsEntity> getRolePermsList(Long roleId) {
        return this.baseMapper.selectRolePermsList(roleId);
    }

    @Override
    public Boolean updateRolePerms(List<Long> menuIds, Long roleId) {
        try{
            // 删除该角色以及有的权限
            this.baseMapper.deleteRolePermsByRoleId(roleId);
            // 添加传来的权限
            this.baseMapper.saveRolePerms(roleId, menuIds);
        } catch (Exception e){
            throw new MyException(e.toString());
        }
        return true;
    }

    @Override
    public void updateStatusByIds(List<Long> asList, Integer status) {
        this.baseMapper.updateStatusByIds(asList, status);
    }

    @Override
    public UserAndAllRole getUserRoleAndAll(String username) {
        UserAndAllRole userRoleAndAll = new UserAndAllRole();
        userRoleAndAll.setUserRoleList(this.baseMapper.selectUserRoleIdByUsername(username));
        userRoleAndAll.setAllRoleList(this.baseMapper.selectRoleAll());
        return userRoleAndAll;
    }
}